﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using System.IO;

namespace COFE.Core.Profile
{
   
    public class DefaultTempManager : ITempManager
    {

        #region Constructor

        public DefaultTempManager(string tempPath, string userName)
        {
            TempPath = Path.Combine(tempPath, userName); 
        }

        #endregion

        #region Methods

        public string NewTempPath()
        {
            int idx = 1;
            while (Directory.Exists(Path.Combine(TempPath, idx.ToString())))
                idx++;

            string dirPath = Path.Combine(TempPath, idx.ToString());
            Directory.CreateDirectory(dirPath);
            return dirPath;
        }

        public string GetTempPath(IFileSystemInfo item)
        {
            if (TempPath == null)
                throw new NotSupportedException("TempPath is not supported.");

            var iitem = item as IInternalFileSystemInfoExA;

            var itemRoot = iitem.getDirectoryRoot(true);
            string rootParse = itemRoot.ParseName;
            string relPth = iitem.getRelativePath(itemRoot);
            string retVal = COFEPath.Combine(TempPath, COFEPath.MakeTempPath(rootParse, relPth));
            System.IO.Directory.CreateDirectory(COFEPath.GetDirectoryName(retVal));
            return retVal;
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        public string TempPath { get; private set; }
        public bool IsEnabled { get { return true; } }

        #endregion
        

        
    }
}
